﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogicUniversityBiz;

namespace LogicUniversity.clerks
{
    public partial class IssueAdjustment : System.Web.UI.Page
    {
        protected void Page_PreInit(Object sender, EventArgs e)
        {
            //(Session["Role"]
            if (Session["Role"] as string != "Clerk")
            {
                Response.Redirect("~/Login.aspx");
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IssueAdjustmentVoucher issueAdjustment = new IssueAdjustmentVoucher();
                List<Catelogue> item = issueAdjustment.getItemCode();
                DropDownList1.DataTextField = "Item_Code";
                DropDownList1.DataSource = item;
                DataBind();

                initList();
            }
            messageLabel.Text = "";
        }

        public void initList()
        {
            List<Discrepancy> disList = new List<Discrepancy>();
            ViewState["DisList"] = disList;

        }
        public void addDiscrepency(List<Discrepancy> disList)
        {
            DiscrepancyView.DataSource = disList;
            DiscrepancyView.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //from session
            EFFascade fs = new EFFascade();
            string empID = Session["UserID"] as string;
            IssueAdjustmentVoucher issueAdjustment = new IssueAdjustmentVoucher();
            String currentTime = DateTime.Now.ToShortDateString();

            string itemCode=DropDownList1.SelectedItem.Value.ToString();
            int adjQty;
            try
            {
                adjQty = Convert.ToInt32(TextBox1.Text.Trim());

                if (Math.Abs(adjQty) == 0)
                {
                    lblError.Text = "Quantity 0 cannot be stored.";
                    return;
                }
            }
            catch (FormatException fe)
            {
                lblError.Text = "Illegal Quantity";
                return;
            }

            lblError.Text =null;

            string reason = TextBox2.Text;
            decimal totalCost = Math.Abs(adjQty) * fs.getUnitPriceOf(itemCode);

            List<Discrepancy> disList = (List<Discrepancy>)ViewState["DisList"];

            if (disList != null)
            {
                btnSave.Visible = true;
                Discrepancy dis = new Discrepancy();
                dis.ItemCode = itemCode;
                dis.QuantityAdj = adjQty;
                dis.Reason = reason;
                dis.TotalCost = totalCost;

                disList.Add(dis);
                ViewState["DisList"] = disList;
            }

            addDiscrepency(disList);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            IssueAdjustmentVoucher issue=new IssueAdjustmentVoucher();
            string empID = Session["UserID"] as string;

            Adjustment_Voucher ad = new Adjustment_Voucher();
            ad.IssuedDate = System.DateTime.Today;
            ad.Conducted_By = empID;

            Voucher_Detail detail;
            foreach(Discrepancy dis in (List<Discrepancy>)ViewState["DisList"])
            {
                detail = new Voucher_Detail();
                detail.Item_Code = dis.ItemCode;
                detail.Quantity_Adjusted = dis.QuantityAdj;
                detail.Reason = dis.Reason;
                detail.Total_Cost = dis.TotalCost;
                ad.Voucher_Detail.Add(detail);
            }
            
            issue.addAdjustmentVoucher(ad);
            Response.Redirect("RequisitionList.aspx");
        }
    }
}